package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.Sensor;
import com.lenovo.smarttraffic.bean.EnvironmentBean;
import com.lenovo.smarttraffic.ui.activity.LineChatActivity;
import com.lenovo.smarttraffic.util.CommonUtil;

/**
 * Created by Caleb.F
 * on 2020-06-29
 * at 14:59
 *
 * @author Caleb.F
 * @Description
 */
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
    private static final String TAG = "MyRecyclerAdapter";
    private Context mContext;
    private EnvironmentBean mlist;
    private EnvironmentBean mlimitData;



    public MyRecyclerAdapter(Context context, EnvironmentBean mlist) {
        mContext = context;
        this.mlist = mlist;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.mTvName.setText(Constant.TITLE_STRINGS[position]);
        switch (position) {
            case 0:
                holder.mTvNum.setText(String.valueOf(mlist.getTem()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getTem(),
                            mlimitData.getTem()));
                }
                break;
            case 1:
                holder.mTvNum.setText(String.valueOf(mlist.getHum()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getHum(),
                            mlimitData.getHum()));
                }
                break;
            case 2:
                holder.mTvNum.setText(String.valueOf(mlist.getLight()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getLight(),
                            mlimitData.getLight()));
                }
                break;
            case 3:
                //co2需要额外的显示
                holder.mTvName.setText(displayCarbon());
                holder.mTvNum.setText(String.valueOf(mlist.getCo()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getCo(),
                            mlimitData.getCo()));
                }
                break;
            case 4:
                holder.mTvNum.setText(String.valueOf(mlist.getPm()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getPm(),
                            mlimitData.getPm()));
                }
                break;
            case 5:
                holder.mTvNum.setText(String.valueOf(mlist.getRoadStatus()));
                if (mlimitData != null) {
                    holder.mLayoutBack.setBackgroundColor(compareData(mlist.getRoadStatus(),
                            mlimitData.getRoadStatus()));
                }
                break;
            default:
                break;
        }
        holder.mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, LineChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constant.LINECHAT_POSITION,Sensor.values()[position]);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

    }

    /**
     * 正确显示CO2
     */
    private SpannableString displayCarbon() {
        SpannableString temp = new SpannableString("CO2");
        temp.setSpan(new AbsoluteSizeSpan(CommonUtil.sp2px(mContext, 40)), 0, 2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        temp.setSpan(new AbsoluteSizeSpan(CommonUtil.sp2px(mContext, 15)), 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        temp.setSpan(new StyleSpan(Typeface.BOLD), 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        temp.setSpan(new SubscriptSpan(), 2, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return temp;
    }

    public void setMlist(EnvironmentBean mlist, EnvironmentBean mlimitData) {
        this.mlist = mlist;
        this.mlimitData = mlimitData;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return 6;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ConstraintLayout mLayoutBack;
        private TextView mTvNum;
        private TextView mTvName;

        public MyViewHolder(View itemView) {
            super(itemView);
            mLayoutBack = itemView.findViewById(R.id.layout_back);
            mTvNum = itemView.findViewById(R.id.tv_num);
            mTvName = itemView.findViewById(R.id.tv_name);
        }
    }

    /**
     * @return 返回红色或者绿色
     */
    private int compareData(int refrenceData, int actualData) {
        if (refrenceData > actualData) {
            return Color.parseColor("#f44336");
        } else {
            return Color.GREEN;
        }

    }


}
